﻿using Foundation;
using MauiBuilder.Services;
using UIKit;

namespace MauiBuilder.Platforms.iOS
{
    public class AppRestartService : IAppRestartService
    {
        public void RestartApp()
        {
            // iOS 不支持真正的应用重启
            // 可以模拟重启效果
            SimulateRestart();
        }

        public async Task RestartAppAsync()
        {
            SimulateRestart();
            await Task.CompletedTask;
        }

        private void SimulateRestart()
        {
            // 清除导航栈并回到初始页面
            if (UIApplication.SharedApplication.KeyWindow?.RootViewController
                is UINavigationController navigationController)
            {
                navigationController.PopToRootViewController(true);
            }

            // 发送通知让应用重置状态
            NSNotificationCenter.DefaultCenter.PostNotificationName(
                new NSString("AppShouldRestart"),
                null);
        }
    }
}
